﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using ecommerce2011.Common;

namespace ecommerce2011.Models
{
    public class OmsClient
    {
        //Dummy
        //servicioAutenticador.servicioAutenticadorService srvc = new servicioAutenticador.servicioAutenticadorService();
        //OSB
        ClientesServiceProxy.wsClientesService srvc = new ClientesServiceProxy.wsClientesService();

        //OSB
        CrearClienteProxy.WsClientesService creadorUsuario = new CrearClienteProxy.WsClientesService();
        //Dummy
        //OSB
        public MembershipUser CreateUser(CrearClienteProxy.customerDTO registro)
        {
            long custid;
            bool retorno;

            creadorUsuario.create(registro, out custid, out retorno);
            
            return GetUser(custid.ToString());
        }

        public string GetUserNameByEmail(string email)
        {
            //TODO
            return "";
        }

        public MembershipUser GetUser(string username)
        {
            var result = srvc.buscarClienteID(username);
            if (result != null)
            {

                string _username = result[0].custid;
                int _providerUserKey = 0;
                string _email = result[0].email;
                string _passwordQuestion = "";
                string _comment = "";
                bool _isApproved = true;
                bool _isLockedOut = false;
                DateTime _creationDate = DateTime.Now;
                DateTime _lastLoginDate = DateTime.Now;
                DateTime _lastActivityDate = DateTime.Now;
                DateTime _lastPasswordChangedDate = DateTime.Now;
                DateTime _lastLockedOutDate = DateTime.Now;

                MembershipUser user = new MembershipUser("ecommerce2011Membership",
                                                            _username,
                                                            _providerUserKey,
                                                            _email,
                                                            _passwordQuestion,
                                                            _comment,
                                                            _isApproved,
                                                            _isLockedOut,
                                                            _creationDate,
                                                            _lastLoginDate,
                                                            _lastActivityDate,
                                                            _lastPasswordChangedDate,
                                                            _lastLockedOutDate);

                return user;
            }
            else
            {
                return null;
            }
        }

        public bool ValidateUser(string username, string password)
        {   
            //Dummy
            //servicioAutenticador.servicioAutenticadorService autentica = new servicioAutenticador.servicioAutenticadorService();
            //OSB
            ClientesServiceProxy.wsClientesService autentica = new ClientesServiceProxy.wsClientesService();
            Encripta encrip = new Encripta();
            //Dummy
            //servicioAutenticador.customer cliente = autentica.autenticar(username, encrip.md5(password));
            //OSB
            ClientesServiceProxy.customerDTO[] retorno = autentica.loginCliente(username, encrip.md5(password));
            ClientesServiceProxy.customerDTO cliente;
            if (retorno != null)
                cliente = retorno[0];
            else
                cliente = new ClientesServiceProxy.customerDTO();

            if (cliente.custid == null)
                return false;
            else
                return true;
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            ClientesServiceProxy.customerDTO[] result = srvc.buscarClienteID(username);
            CrearClienteProxy.customer cliente = new CrearClienteProxy.customer();
            if (result != null && result[0] != null)
            {
                if (result[0].password != (new Encripta().md5(oldPassword)))
                    return false;

                cliente.addressCollection = null;
                cliente.creditcardnumber = result[0].creditcardnumber;
                cliente.creditcardtype = result[0].creditcardtype;
                cliente.custid = result[0].custid;
                cliente.email = result[0].email;
                cliente.fname = result[0].fname;
                cliente.lname = result[0].lname;
                cliente.password = new Encripta().md5(newPassword);
                cliente.phonenumber = result[0].phonenumber;
                cliente.status = result[0].status;
                creadorUsuario.edit(cliente);

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}